package com.cloud.controller;

import com.cloud.mapper.Dictionaries;
import com.cloud.mapper.DictionariesMapper;
import com.cloud.observer.annotation.RedisFileUtils;
import com.cloud.service.DealerService;
import org.apache.commons.net.ntp.TimeStamp;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.cloud.controller
 * @date:2021/4/19
 */
@Controller
public class TestController {


    @Autowired
    DealerService dealerService;

//    @Autowired
//    private Connection HBaseConnection;

    @Autowired
    RedisTemplate redisTemplate;

//    @Autowired
//    RedissonClient redisson;

    @Autowired
    DictionariesMapper dictionariesMapper;


    @GetMapping(value = "map")
    public void map(){
        List<Dictionaries> list = dictionariesMapper.selectAll();
        list.stream().forEach(dictionaries -> {
            redisTemplate.opsForHash().put("Dictionaries",dictionaries.getSuffix(),dictionaries.getMap());
        });

        redisTemplate.opsForHash().put("DetailDictionaries","1","1");
        redisTemplate.opsForHash().put("DetailDictionaries","2","2");
        redisTemplate.opsForHash().put("DetailDictionaries","3","2");
        redisTemplate.opsForHash().put("DetailDictionaries","6","1");
        redisTemplate.opsForHash().put("DetailDictionaries","12","3");
        redisTemplate.opsForHash().put("DetailDictionaries","8","3");
    }



    @GetMapping(value = "create")
    public void create(){
        java.sql.Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //通过驱动管理类获取数据库链接
            for(int i =0 ;i<43;i++){
                connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/stock_"+i+"?characterEncoding=utf-8", "root", "123456");
                for (int a = 0;a<43;a++) {
                    String sql3 = "CREATE TABLE `_stock_circulation_detail_"+i+"_"+a+"_3` (\n" +
                            "  `_link_id` int NOT NULL AUTO_INCREMENT,\n" +
                            "  `_barcode_content` varchar(50) DEFAULT NULL COMMENT '码内容',\n" +
                            "  `_status` int DEFAULT NULL COMMENT '1.入库 2.出库 3.其他出库 \\n6.退库\\n8.sd发货 \\n12.门店入库',\n" +
                            "  `_organization_type` int DEFAULT NULL COMMENT '0.��ҵ 1.������ 2.�ն˵�',\n" +
                            "  `_organization_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��ҵcode��������code���ն�code',\n" +
                            "  `_organization_name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '操作组织名称',\n" +
                            "  `_product_code` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,\n" +
                            "  `_product_name` text CHARACTER SET utf8 COLLATE utf8_general_ci,\n" +
                            "  `_dealers_type` int DEFAULT NULL COMMENT '0.��ҵ 1.������ 2.�ն˵�',\n" +
                            "  `_dealers_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��ҵcode��������code���ն�code',\n" +
                            "  `_dealers_name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '客户名称',\n" +
                            "  PRIMARY KEY (`_link_id`) USING BTREE\n" +
                            ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci";
                    connection.createStatement().execute(sql3);


                    String sql2 = "CREATE TABLE `_stock_circulation_detail_"+i+"_"+a+"_2` (\n" +
                            "  `_link_id` int NOT NULL AUTO_INCREMENT,\n" +
                            "  `_barcode_content` varchar(50) DEFAULT NULL COMMENT '码内容',\n" +
                            "  `_status` int DEFAULT NULL COMMENT '1.入库 2.出库 3.其他出库 \\n6.退库\\n8.sd发货 \\n12.门店入库',\n" +
                            "  `_organization_type` int DEFAULT NULL COMMENT '0.��ҵ 1.������ 2.�ն˵�',\n" +
                            "  `_organization_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��ҵcode��������code���ն�code',\n" +
                            "  `_organization_name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '操作组织名称',\n" +
                            "  `_product_code` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,\n" +
                            "  `_product_name` text CHARACTER SET utf8 COLLATE utf8_general_ci,\n" +
                            "  `_dealers_type` int DEFAULT NULL COMMENT '0.��ҵ 1.������ 2.�ն˵�',\n" +
                            "  `_dealers_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��ҵcode��������code���ն�code',\n" +
                            "  `_dealers_name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '客户名称',\n" +
                            "  PRIMARY KEY (`_link_id`) USING BTREE\n" +
                            ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci";
                    connection.createStatement().execute(sql2);
                    String sql1 = "CREATE TABLE `_stock_circulation_detail_"+i+"_"+a+"_1` (\n" +
                            "  `_link_id` int NOT NULL AUTO_INCREMENT,\n" +
                            "  `_barcode_content` varchar(50) DEFAULT NULL COMMENT '码内容',\n" +
                            "  `_status` int DEFAULT NULL COMMENT '1.入库 2.出库 3.其他出库 \\n6.退库\\n8.sd发货 \\n12.门店入库',\n" +
                            "  `_organization_type` int DEFAULT NULL COMMENT '0.��ҵ 1.������ 2.�ն˵�',\n" +
                            "  `_organization_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��ҵcode��������code���ն�code',\n" +
                            "  `_organization_name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '操作组织名称',\n" +
                            "  `_product_code` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,\n" +
                            "  `_product_name` text CHARACTER SET utf8 COLLATE utf8_general_ci,\n" +
                            "  `_batch_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��������',\n" +
                            "  `_product_line_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '������code',\n" +
                            "  `_product_line_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '������',\n" +
                            "  `_workshop_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��������code',\n" +
                            "  `_workshop_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��������',\n" +
                            "  `_product_team_code` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��������code',\n" +
                            "  `_product_team` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��������',\n" +
                            "  `_warehouse_code` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '�ֿ�code',\n" +
                            "  `_warehouse_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '��Դ�ֿ�',\n" +
                            "  PRIMARY KEY (`_link_id`) USING BTREE\n" +
                            ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci";
                    connection.createStatement().execute(sql1);


                    String sql0 ="CREATE TABLE `_stock_circulation_"+i+"_"+a+"` (\n" +
                            "  `_id` int NOT NULL AUTO_INCREMENT COMMENT '流转记录表id',\n" +
                            "  `_barcode_content` varchar(50) DEFAULT NULL COMMENT '码内容',\n" +
                            "  `_status` int DEFAULT NULL COMMENT '1.入库 2.出库 3.其他出库 \\n6.退库\\n8.sd发货 \\n12.门店入库',\n" +
                            "  `_operate_time` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作时间',\n" +
                            "  `_operate_name` varchar(200) DEFAULT NULL COMMENT '操作人',\n" +
                            "  `_workid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '业务单号',\n" +
                            "  `_link_id` int DEFAULT NULL,\n" +
                            "  PRIMARY KEY (`_id`) USING BTREE,\n" +
                            "  KEY `INDEX_barcode_content` (`_barcode_content`),\n" +
                            "  KEY `INDEX_operate_time` (`_operate_time`)\n" +
                            ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='库存流转表'";
                    connection.createStatement().execute(sql0);

                }
            }
        }catch (Exception e) {


        }
    }



//    @GetMapping(value = "/r")
//    public void setRedisson() {
//        RLock lock = redisson.getLock("OutBillScanSetLock1111111");
//        try {
//
//            lock.lock();
//            Thread.sleep(40000);
////            lock.lock(10000, TimeUnit.SECONDS);
//            redisTemplate.opsForZSet().add("OutBillScanSet", "HTTPS://B.AAX1.CN/BVMA2HKFJBH93F1EO55A", 1000);
//            lock.unlock();
//        } catch (Exception e) {
//            if (null != lock) {
//                lock.unlock();
//            }
//        }
//
//    }

//    @GetMapping(value = "/b")
//    public void test2(HttpServletRequest httpRequest) throws Exception {
//        try (Table table = HBaseConnection.getTable(TableName.valueOf("libin"))) {//获取表连接
//            //配置一条数据
//            // 行键
//            Put put = new Put(Bytes.toBytes("key主键"));
//            put.addColumn(Bytes.toBytes("列族"), Bytes.toBytes("列"), Bytes.toBytes("值"));
//            table.put(put);
//        }
//    }

    @PostMapping(value = "/a")
    public void test(HttpServletRequest httpRequest) throws Exception {

        List<String> list = new ArrayList<>();
        list.add("HTTPS://AAX1.CN/6JZPET5O4VTY0ZY2QJC4");
        list.add("HTTPS://AAX1.CN/WJZ4M41OFAT2CZ10VCDF");
        list.add("HTTPS://AAX1.CN/B0ZA4U8ODHUR0AHPBJWR");
        list.add("HTTPS://AAX1.CN/JJZ5FLVORJUDHAG8I86N");
        list.add("HTTPS://AAX1.CN/K2ZDL4NOY7U3BA1VQMBA");
        list.add("HTTPS://AAX1.CN/K2ZV5X6O57U87AEGZF9D");
        list.add("HTTPS://AAX1.CN/K1ZLVV1OU3U1YBLTJTCF");
        list.add("HTTPS://AAX1.CN/RVZX0V6OL3UVTB6UU3MK");
        list.add("HTTPS://AAX1.CN/93ZDVGVONLURHCWYGX4F");
        list.add("HTTPS://AAX1.CN/CKZ91FGODKUFNCUYTRKA");
        list.add("HTTPS://AAX1.CN/GKZCA16O8MU4ADSDH1AA");
        list.add("HTTPS://AAX1.CN/LHZHHLGOB4UTEDYMW6J4");
        list.add("HTTPS://AAX1.CN/3BZW7JGOV0UE3ESBMLJA");
        list.add("HTTPS://AAX1.CN/XYZ4JKUO51UKXF8DD2WR");


        list.stream().forEach(s -> {
            String last = s.substring(s.length() - 3, s.length());
            String key = "bottle-" + RedisFileUtils.calculate(last);
            String value = RedisFileUtils.calculate(RedisFileUtils.getCode(s));
            System.out.println(key);
            System.out.println(value);
            redisTemplate.opsForSet().add(key, value);
        });


//        String  a = httpRequest.getParameter("aaaaa");
//        InputStream in = httpRequest.getInputStream();
//        File tmpFile = new File("/Users/libin/Desktop/123.zip");
//        File copy = new File(tmpFile.getPath().substring(0,tmpFile.getPath().lastIndexOf("/")));
////        File copy = new File(filePath);
//        if (!copy.exists()) {
//            copy.mkdirs();
//        }
//        byte[] tmp_byte = new byte[1024];
//        FileOutputStream fileout = null;
//        int tmp = -1;
//        fileout = new FileOutputStream(tmpFile);
//        while ((tmp = in.read(tmp_byte)) > -1) {
//            fileout.write(tmp_byte, 0, tmp);
//        }
//
//        System.out.println("123123");
    }


//    @PostMapping(value = "/b")
//    public void test2() throws Exception {
//        dealerService.updateAll();
//    }


    public static void writeFile(String filePath, Collection<String> lineList) throws Exception {
        File copy = new File(filePath.substring(0, filePath.lastIndexOf("/")));
//        File copy = new File(filePath);
        if (!copy.exists()) {
            copy.mkdirs();
        }
        try {
            FileOutputStream fout = new FileOutputStream(filePath, true);
            for (String string : lineList) {
                fout.write(string.getBytes("utf-8"));
            }
            fout.flush();
            fout.close();
        } catch (Exception e) {
            throw e;
        }
    }


    private static TableName tableName = TableName.valueOf("test");
    private static final String cf = "ke";

    public static void main(String[] arg) throws Exception {
        Configuration config = HBaseConfiguration.create();

        config.set("hbase.zookeeper.quorum", "hmaster");
//        config.set("zookeeper.znode.parent", "/hbase");
        config.set("hbase.zookeeper.property.clientPort", "2181");

        System.out.println("开始连接hbase");
        Connection connect = ConnectionFactory.createConnection(config);
        System.out.println(connect.isClosed());
        Admin admin = connect.getAdmin();
        System.out.println("连接成功");
//        admin.listTableNames();
        Table table = connect.getTable(TableName.valueOf("hbase_1102"));
        System.out.println("获取表数据成功");
//        for i :table.getScanner().iterator();


//        if (!admin.tableExists(tableName)) {
//            admin.createTable(new HTableDescriptor(tableName).addFamily(new HColumnDescriptor(cf)));
//        }
//        System.out.println("建表数据成功");
//
//        Table table = connect.getTable(tableName);
        TimeStamp ts = new TimeStamp(new Date());
        Date date = ts.getDate();
        Get get = new Get(Bytes.toBytes("001"));
        Result result = table.get(get);
        List<Cell> cells = result.listCells();
        Map<String, String> objectMap = new HashMap<>();
        for (Cell cell : cells) {
            String qualifier = new String(CellUtil.cloneQualifier(cell));
            String value = new String(CellUtil.cloneValue(cell), "UTF-8");
            objectMap.put(qualifier, value);
        }
        table.close();
        connect.close();
    }
}
